<script setup>
import { ref, onMounted } from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import myAxios from '../../../plugins/myAxios'

// 反馈列表数据
const feedbackList = ref([])
// 分页参数
const pagination = ref({
  total: 0,
  currentPage: 1,
  pageSize: 10
})
// 搜索条件
const searchForm = ref({
  feedbackType: '',
  status: '',
  dateRange: []
})
// 回复对话框
const replyDialog = ref(false)
const currentFeedback = ref({})
const replyContent = ref('')

// 反馈类型选项
const feedbackTypeOptions = [
  { label: '考试内容问题', value: 1 },
  { label: '测试建议', value: 2 },
  { label: '其他', value: 3 }
]

// 状态选项
const statusOptions = [
  { label: '待处理', value: 0 },
  { label: '已回复', value: 1 },
  { label: '已关闭', value: 2 }
]

// 获取反馈列表
const getFeedbackList = async () => {
  try {
    const params = {
      pageNum: pagination.value.currentPage,
      pageSize: pagination.value.pageSize,
      feedbackType: searchForm.value.feedbackType || null,
      status: searchForm.value.status || null,
      startTime: searchForm.value.dateRange?.[0] || null,
      endTime: searchForm.value.dateRange?.[1] || null
    }
    const res = await myAxios.get('/admin/feedback/page', { params })
    feedbackList.value = res.records
    pagination.value.total = res.total
  } catch (error) {
    ElMessage.error('获取反馈列表失败：' + error)
  }
}

// 提交回复
const handleReply = async () => {
  if (!replyContent.value.trim()) {
    ElMessage.warning('请输入回复内容')
    return
  }
  try {
    await myAxios.post('/admin/feedback/reply', {
      feedbackId: currentFeedback.value.feedbackId,
      replyContent: replyContent.value
    })
    ElMessage.success('回复成功')
    replyDialog.value = false
    replyContent.value = ''
    getFeedbackList() // 刷新列表
  } catch (error) {
    ElMessage.error('回复失败：' + error)
  }
}

// 关闭反馈
const handleClose = async (row) => {
  try {
    await ElMessageBox.confirm('确认关闭该反馈？', '提示', {
      type: 'warning'
    })
    await myAxios.post(`/admin/feedback/close`,{
      feedbackId: Number(row.feedbackId),
    })
    ElMessage.success('关闭成功')
    getFeedbackList() // 刷新列表
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('关闭失败：' + error)
    }
  }
}

// 重置搜索表单
const resetSearch = () => {
  searchForm.value = {
    feedbackType: '',
    status: '',
    dateRange: []
  }
  getFeedbackList()
}

onMounted(() => {
  getFeedbackList()
})
</script>

<template>
  <div class="feedback-admin">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="反馈类型" style="width: 200px">
          <el-select v-model="searchForm.feedbackType" clearable>
            <el-option
              v-for="item in feedbackTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" style="width: 160px">
          <el-select v-model="searchForm.status" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="提交时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getFeedbackList">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 列表区域 -->
    <el-card class="list-card">
      <el-table :data="feedbackList" border stripe>
        <el-table-column prop="feedbackId" label="反馈ID" width="80" />
        <el-table-column prop="studentId" label="学生ID" width="100" />
        <el-table-column label="反馈类型" width="120">
          <template #default="{ row }">
            {{ feedbackTypeOptions.find(item => item.value === row.feedbackType)?.label }}
          </template>
        </el-table-column>
        <el-table-column prop="feedbackContent" label="反馈内容" show-overflow-tooltip />
        <el-table-column prop="submitTime" label="提交时间" width="160" />
        <el-table-column prop="replyContent" label="回复内容" show-overflow-tooltip>
          <template #default="{ row }">
            <span v-if="row.replyContent">{{ row.replyContent }}</span>
            <el-tag v-else size="small" type="info">暂无回复</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="replyTime" label="回复时间" width="160">
          <template #default="{ row }">
            {{ row.replyTime || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="replyBy" label="回复人" width="120">
          <template #default="{ row }">
            {{ row.replyBy || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 0 ? 'warning' : row.status === 1 ? 'success' : 'info'">
              {{ statusOptions.find(item => item.value === row.status)?.label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              :disabled="row.status === 2"
              @click="currentFeedback = row; replyDialog = true"
            >
              回复
            </el-button>
            <el-button
              type="danger"
              link
              :disabled="row.status === 2"
              @click="handleClose(row)"
            >
              关闭
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :total="pagination.total"
        @current-change="getFeedbackList"
        @size-change="getFeedbackList"
      />
    </el-card>

    <!-- 回复对话框 -->
    <el-dialog
      v-model="replyDialog"
      title="回复反馈"
      width="500px"
    >
      <el-form label-width="80px">
        <el-form-item label="反馈内容">
          <div class="feedback-content">{{ currentFeedback.feedbackContent }}</div>
        </el-form-item>
        <el-form-item label="回复内容">
          <el-input
            v-model="replyContent"
            type="textarea"
            rows="4"
            placeholder="请输入回复内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="replyDialog = false">取消</el-button>
        <el-button type="primary" @click="handleReply">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.feedback-admin {
  padding: 20px;
}
.search-card {
  margin-bottom: 20px;
}
.list-card {
  margin-bottom: 20px;
}
.feedback-content {
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
}
.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>
